import pymysql
"""
数据库操作方法
"""
# mysql -h10.93.11.184 -P 3306 -uhcsdb -p}]rKM:ED


def insert_mysql(data):
    db = pymysql.connect("10.93.11.184", "hcsdb", "}]rKM:ED", "train_qa")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    # data = ('qunar', 15, 15, 0, 0, '成功', '测试报告地址', '创建时间')
    # SQL 插入语句
    sql = "INSERT INTO uranus(builduser, casetotal, successes, " \
          "faileds, errors, result, report, createtime) VALUES " \
          "('%s',%s,%s,%s,%s,'%s','%s','%s')" % data

    try:
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
        print('插入成功')
    except Exception as e:
        # 如果发生错误则回滚
        print(e)
        db.rollback()

    # 关闭数据库连接
    db.close()


if __name__ == '__main__':
    insert_mysql()